package com.cskt.common.utils;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.cskt.common.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 阿里云短信验证码工具类
 *
 * @author admin
 * @date 2023/04/13 16:22
 **/

public class AliyunSmsUtil {
    private static final Logger log = LoggerFactory.getLogger(AliyunSmsUtil.class);

    /**
     * 发送验证码
     *
     * @param phone
     * @param code
     * @return
     */
    public static boolean sendCaptcha(String phone, String code) throws Exception {
        Config config = new Config()
                .setAccessKeyId("LTAI5tHZyQEu3hmkrzGKy1do")
                .setAccessKeySecret("oqusanoJA8N8kWKO4vPqNCqvrI1fl6");
        config.endpoint = "dysmsapi.aliyuncs.com";
        Client client = new Client(config);
        SendSmsRequest request = new SendSmsRequest()
                .setSignName("阿里云短信测试")
                .setTemplateCode("SMS_154950909")
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":\"" + code + "\"}");
        SendSmsResponse response = client.sendSms(request);
        log.info("响应数据：{}",response.body.code);
        if (response.body.code.equals(Constants.Sms.ALI_SMS_RESULT)) {
            log.info("短信送成功");
            return true;
        }
        log.info("短信发送失败，{}",response.body.code);
        return false;
    }


}
